﻿// <copyright file="SolutionShape.cs" company="ThorIndustry">Copyright (c) ThorIndustry. All rights reserved.</copyright>

public class SolutionShape : UnityEngine.MonoBehaviour
{
    public UnityEngine.Material ShapeMaterial;
    public UnityEngine.GameObject ShapePrefab;
    public float borderSize = 0.1f;

    private UnityEngine.Transform myTransform;

    private UnityEngine.Vector3 center;

    public PuzzleSolutionShape PuzzleSolutionShape
    {
        get;
        set;
    }

    public Shape[] Shapes
    {
        get;
        set;
    }

    public UnityEngine.Color Color
    {
        get;
        set;
    }

    public float ShapeWidth
    {
        get;
        set;
    }

    public float ShapeHeight
    {
        get;
        set;
    }

    public UnityEngine.Vector3 GetLocalCenter()
    {
        return (UnityEngine.Vector3)(this.transform.localToWorldMatrix * this.center);
    }

    public void Initialize()
    {
        this.myTransform = this.transform;

        float maxX = float.MinValue;
        float maxY = float.MinValue;
        float minX = float.MaxValue;
        float minY = float.MaxValue;

        this.Shapes = new Shape[this.PuzzleSolutionShape.SolutionShapes.Length];
        for (int index = 0; index < this.PuzzleSolutionShape.SolutionShapes.Length; ++index)
        {
            this.Shapes[index] = PuzzleView.GenerateShape(this.PuzzleSolutionShape.SolutionShapes[index], this.Color, this.myTransform, this.ShapePrefab);
            this.Shapes[index].ShapeMaterial = this.ShapeMaterial;
            this.Shapes[index].Height = 0;
            this.Shapes[index].InnerDisplacement = borderSize;
            this.Shapes[index].CanMove = false;

            this.Shapes[index].SelectionIndex = -1;

            this.Shapes[index].Initialize();

            minX = UnityEngine.Mathf.Min(this.Shapes[index].MinPoint.x, minX);
            maxX = UnityEngine.Mathf.Max(this.Shapes[index].MaxPoint.x, maxX);
            minY = UnityEngine.Mathf.Min(this.Shapes[index].MinPoint.y, minY);
            maxY = UnityEngine.Mathf.Max(this.Shapes[index].MaxPoint.y, maxY);
        }

        this.ShapeWidth = maxX - minX;
        this.ShapeHeight = maxY - minY;

        this.center.x = maxX - (this.ShapeWidth / 2);
        this.center.y = maxY - (this.ShapeHeight / 2);
    }
}
